using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using vs.Core;
using vs.Authentication;

namespace Forum
{
    public partial class topic_new : System.Web.UI.UserControl
    {
        private void RegistJavascriptEditor()
        {
            SmileController smile = new SmileController();

            string jsInsertSmile = smile.js_insert_smilie(txtContent.ClientID);
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "jsInsertSmile", jsInsertSmile);
            lblSmilies.Text = smile.list_images_smilies();

            string jsInsertEditor = smile.js_insert_editor(txtContent.ClientID);
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "jsInsertEditor", jsInsertEditor);
            lblEditor.Text = smile.list_images_editor();
        }

        private bool validating()
        {
            txtTopicName.Text = Utils.KillScript(txtTopicName.Text.Trim());
            txtContent.Text = Utils.KillScript(txtContent.Text.Trim());

            if (txtTopicName.Text == "")
            {
                lblMessage.Text = "Ban phai nhap ten chu de!";
                lblMessage.Visible = true;
                return false;
            }
            if (txtContent.Text == "")
            {
                lblMessage.Text = "Noi dung bai viet khong duoc bo trang!";
                lblMessage.Visible = true;
                return false;
            }

            return true;
        }

        private void RegisterScriptIMG()
        {
            string js = @"
<script language=javascript>
    function popup2 (name,width,height) { 
         var options = ""toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1, width=""+width+"",height=""+height; 
         Cal2=window.open(name,""popup"",options); 
    }

    function SetImage(val)
    {
        val = '[img]' + val + '[/img]';
        var content = document.getElementById('" + txtContent.ClientID + @"');
        if (content.createTextRange && content.caretPos){
            var caretPos = content.caretPos;
            caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? val + ' ' : val;
        } else { content.value += val; }
        content.focus();
    }
</script>
";

            lnkInsertImage.Visible = true;
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "setvalue", js);
            string popup = ResolveUrl("~/portal/Galery/ImageGalery.aspx");
            lnkInsertImage.NavigateUrl = "javascript:popup2('" + popup + "', 500, 400);";
        }
        private void LoadStaffList()
        {
            if (Authenticate.CurrentUser.Staff)
            {
                ddlStaff.Visible = true;
                UserDB.FillStaff(ddlStaff);
                RegisterScriptIMG();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    if (!HttpContext.Current.User.Identity.IsAuthenticated)
                        Response.Redirect(UrlProvider.AdminUrl("user", "login"));

                    LoadStaffList();
                    RegistJavascriptEditor();
                }
                catch (Exception ex)
                {
                    lblMessage.Text = ex.Message;
                    lblMessage.Visible = true;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (!validating()) return;
                engine_forum engine = new engine_forum();
                string forumID = Request.QueryString["ID"];

                string username = HttpContext.Current.User.Identity.Name;
                if (Authenticate.CurrentUser.Staff)
                    username = ddlStaff.SelectedValue;

                int topicID = engine.InsertTopic(txtTopicName.Text.Trim(),
                    txtContent.Text.Trim(), forumID, username);

                engine_xml xml = new engine_xml();
                int pg_count = engine.CountPageForum(forumID);
                xml.RemoveCache_TopicList(forumID, pg_count - 1);
                DataCache.RemoveCache(topic_lastest.CACHE_LATEST_TOPIC);

                //redirect
                string modid = PortalUtils.CurrentModuleID;
                ModSettingController db = new ModSettingController();
                string topicdesk = db.Load(modid, "topicdesk");

                string url = UrlProvider.DesktopUrl(topicdesk, topicID.ToString());
                Response.Redirect(url);
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
                lblMessage.Visible = true;
            }
        }
    }
}